//
//  DYCustomViewController.m
//  colorTest
//
//  Created by 丁益 on 2017/4/2.
//  Copyright © 2017年 celi. All rights reserved.
//  自定义模式

#import "DYCustomViewController.h"
#import "CLOfflineCacheTool.h"
#import "DYcolorMode.h"
#import "MJExtension.h"
#import "CLBaseTextField.h"
#import "DYSelectColorView.h"

@interface DYCustomViewController () <DYSelectColorViewDelegate>
@property (nonatomic ,strong)  NSMutableArray *colorsArray;
@property (nonatomic ,weak)   CLBaseTextField *nameText;
@property (nonatomic ,weak)   UILabel *secValueLabel;
@property (nonatomic ,weak)   UILabel *minValueLabel;
@property (nonatomic ,weak)   UIButton *colorTempBtn;
@property (nonatomic ,weak)   UIImageView *selectImage;

@end

@implementation DYCustomViewController

- (NSMutableArray *)colorsArray
{
    if (_colorsArray == nil) {
        _colorsArray = [NSMutableArray array];
        
        //从缓存文件中种加载颜色值
        NSArray *array ;
        array = [CLOfflineCacheTool readColorsPlistFileMember:mID cacheType:colorsType];
        
        
        for (NSDictionary *dict in array) {
            DYcolorMode *mode = [DYcolorMode mj_objectWithKeyValues:dict];
            [_colorsArray addObject:mode];
        }
        
        if (_colorsArray.count == 0) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (int i =0; i<12; i++) {
                DYcolorMode *mode = [[DYcolorMode alloc]init];
                mode.redValue = 255;//i;
                mode.greenValue = 255;//i+10;
                mode.blueValue = 255;//i+20;
                [tempArray addObject:mode.mj_keyValues];
            }
            [CLOfflineCacheTool saveContactPersonPlistFileData: tempArray member:mID cacheType:colorsType];
        }
    }
    
    return _colorsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"自定义模式";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 84, 50, 30)];
    nameLabel.text = @"名称";
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:nameLabel];
    
    CGFloat nameTextX = CGRectGetMaxX(nameLabel.frame)+20;
    CGFloat nameTextW = sWidth - nameTextX - 30;
    CLBaseTextField *nameText = [CLBaseTextField BaseInputBox];
    nameText.frame = CGRectMake(nameTextX, 84, nameTextW, 35);
    nameText.placeholder = @"自定义模式";
    self.nameText = nameText;
    [self.view addSubview:nameText];
    
    UIButton *saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(onClickedSavebtn:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *saveBarItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
    
    self.navigationItem.rightBarButtonItem = saveBarItem;
    

    [self colorsArray];
    
    [self creatColor];
    
    
}

- (void)onClickedSavebtn:(UIButton*)btn
{
    DLog(@"onClickedSavebtn");
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (int i =0; i<_colorsArray.count; i++) {
        DYcolorMode *mode = _colorsArray[i];
        [tempArray addObject:mode.mj_keyValues];
    }
    [CLOfflineCacheTool saveContactPersonPlistFileData: tempArray member:mID cacheType:colorsType];
}

// 创建颜色方块
- (void)creatColor
{
    CGFloat btnW = (sWidth-105)/4;
    CGFloat btnH = btnW;
    CGFloat btnY = 0;
    
    UIImageView *selectImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"member_select"]];
    self.selectImage = selectImage;
    
    for (int i=0;i<12;i++) {
        btnY = CGRectGetMaxY(self.nameText.frame)+20 + i/4*(btnH+15);
        UIButton *colorBtn = [[UIButton alloc]initWithFrame:CGRectMake(30+i%4*(btnW+15), btnY, btnW, btnH)];
        colorBtn.tag = i;
        colorBtn.backgroundColor = [UIColor whiteColor];
        colorBtn.layer.shadowColor = [UIColor grayColor].CGColor;
        colorBtn.layer.shadowOffset = CGSizeMake(0, 1);
        colorBtn.layer.shadowOpacity = 0.5;
        colorBtn.layer.borderColor = [UIColor colorWithWhite:0.5 alpha:0.8].CGColor;
        colorBtn.layer.borderWidth = 0.5;
        colorBtn.layer.cornerRadius = 2;
        colorBtn.layer.masksToBounds = YES;
        
        DYcolorMode *modeColor = self.colorsArray[i];
        colorBtn.backgroundColor = DYColor(modeColor.redValue, modeColor.greenValue, modeColor.blueValue);//[UIColor whiteColor];//DYRandomColor;
        [colorBtn addTarget:self action:@selector(colorBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:colorBtn];
        
        if (i == 0) {
            selectImage.frame = colorBtn.frame;
            [self.view addSubview:selectImage];
        }
    }
    
    
    CGFloat minLabelY = CGRectGetMaxY(self.nameText.frame)+20 + btnH*4;
    UILabel *minLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, minLabelY, 50, 30)];
    minLabel.text = @"分钟:";
    [self.view addSubview:minLabel];
    CGFloat minValueLabelX = CGRectGetMaxX(minLabel.frame);
    UILabel *minValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(minValueLabelX, minLabelY, 50, 30)];
    minValueLabel.text = @"30min";
    self.minValueLabel = minValueLabel;
    [self.view addSubview:minValueLabel];
    
    CGFloat minSliderY = CGRectGetMaxY(minLabel.frame);
    UISlider *minSlider = [[UISlider alloc]initWithFrame:CGRectMake(30,minSliderY,sWidth-60, 20)];
    minSlider.value = 0.5;
    [minSlider addTarget:self action:@selector(minSliderChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:minSlider];
    
    CGFloat secLabelY = CGRectGetMaxY(minSlider.frame)+10;
    UILabel *secLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, secLabelY, 50, 30)];
    secLabel.text = @"秒钟:";
    [self.view addSubview:secLabel];
    CGFloat secValueLabelX = CGRectGetMaxX(secLabel.frame);
    UILabel *secValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(secValueLabelX, secLabelY, 50, 30)];
    secValueLabel.text = @"30s";
    self.secValueLabel = secValueLabel;
    [self.view addSubview:secValueLabel];
    
    CGFloat secSliderY = CGRectGetMaxY(secLabel.frame);
    UISlider *secSlider = [[UISlider alloc]initWithFrame:CGRectMake(30, secSliderY,sWidth-60, 20)];
    secSlider.value = 0.5;
    [secSlider addTarget:self action:@selector(secSliderChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:secSlider];
}

- (void)minSliderChanged:(UISlider*) slider
{
    
    self.minValueLabel.text = [NSString stringWithFormat:@"%.0lfmin",slider.value*59];
    DLog(@"minSliderChanged=%f",slider.value);
}

- (void)secSliderChanged:(UISlider*)slider
{
    self.secValueLabel.text = [NSString stringWithFormat:@"%.0lfs",slider.value*60];
     DLog(@"secSliderChanged=%f",slider.value);
}

- (void)colorBtnOnclick:(UIButton*)btn
{
    UIColor *color = btn.backgroundColor;
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    DLog(@"Red: %f", components[0]);
    DLog(@"Green: %f", components[1]);
    DLog(@"Blue: %f", components[2]);
    DYSelectColorView *selectColorView = [[DYSelectColorView alloc]initWithSelectColorWithRedValue:[NSString stringWithFormat:@"%0.0f",components[0]*255] blueValue:[NSString stringWithFormat:@"%0.0f",components[2]*255] greenValue:[NSString stringWithFormat:@"%0.0f",components[1]*255]];
    selectColorView.delegate = self;
    [selectColorView showColorView];
    
    self.selectImage.frame = btn.frame;
    [self.view bringSubviewToFront:self.selectImage];
    self.colorTempBtn = btn;
    [self.view addSubview:selectColorView];
}


#pragma DYSelectColorViewDelegate
- (void)selectColorWithRedValue:(NSString *)redValue blueValue:(NSString *)blueValue greenValue:(NSString *)greenValue
{
    self.colorTempBtn.backgroundColor = DYColor([redValue integerValue], [greenValue integerValue], [blueValue integerValue]);
    DYcolorMode *mode = [[DYcolorMode alloc]init];
    mode.redValue = redValue.integerValue;
    mode.greenValue = greenValue.integerValue;
    mode.blueValue = blueValue.integerValue;
    
    self.colorsArray[self.colorTempBtn.tag] = mode;
}



- (void) nameView
{
    CGFloat nameLabelY = 20;
    CGFloat nameLabelX = 20;
    CGFloat nameWidth = 80;
    UILabel *namelabel = [[UILabel alloc]initWithFrame:CGRectMake(nameLabelX,nameLabelY,nameWidth,20)];
    [self.view addSubview:namelabel];
    
    CGFloat textFieldX = CGRectGetMaxX(namelabel.frame)+20;
    CGFloat textFieldY = CGRectGetMaxY(namelabel.frame)+10;
    CGFloat textFieldWidth = sWidth - textFieldX - 40;
    CGFloat textFieldHeight =  35;
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(textFieldX, textFieldY, textFieldWidth, textFieldHeight)];
    
    [self.view addSubview:textField];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
